Uec3.PartUploadManageWindow = Ext.extend(Ext.app.Module,{
	id: 'partUploadManage',

   init : function(initParams){
   
   },
   
   getComponent:function(params){
   	      

   	      var statusArry  = [
              ['1','待处理'],
              ['2','成功处理'],
              ['3','处理失败']
            ];

  
   var store_status = new Ext.data.ArrayStore({
        fields: ['id', 'name'],
        data : statusArry
    });
    
     var userStatus_comb = new Ext.form.ComboBox({
	    fieldLabel:'数据状态',
        store: store_status,
        displayField:'name',
        valueField:'id',
        hiddenName:'status',
        typeAhead: true,
        mode: 'local',
        forceSelection: true,
        triggerAction: 'all',
        emptyText:'请选择',
        anchor : '100%',
        selectOnFocus:true
    }); 
   	      
   		  var search_form = new Ext.FormPanel({
   	       		frame:true,
   	      		border:false,
   	       	    items:[
   	       	      {
   	       	        layout:'column',
   	       	        items:[
   	       	           {
   	       	             columnWidth:0.2,
   	       	             items:[
   	       	                {
   	       	                  layout:'form',
   	       	                  labelWidth:55,
   	       	                  items:[
   	       	                    {
   	       	                      xtype:'textfield',
   	       	                      fieldLabel:'公司名称',
   	       	                      name:'companyName',
   	       	                      width:60,
   	       	                      anchor:'100%'
   	       	                    }
   	       	                  ]
   	       	                }
   	       	             ]
   	       	           },{
   	       	             columnWidth:0.2,
   	       	             items:[
   	       	                {
   	       	                  layout:'form',
   	       	                  labelWidth:55,  	       	                  
   	       	                  items:[
   	       	                    {
   	       	                      xtype:'textfield',
   	       	                      fieldLabel:'零件编码',
   	       	                      name:'code',  	       	                      
   	       	                      anchor:'100%'
   	       	                    }
   	       	                  ]
   	       	                }
   	       	             ]
   	       	           },{
   	       	             columnWidth:0.2,
   	       	             items:[
   	       	                {
   	       	                  layout:'form',
   	       	                  labelWidth:55,  	       	                  
   	       	                  items:[
   	       	                    {
   	       	                      xtype:'textfield',
   	       	                      fieldLabel:'零件名称',
   	       	                      name:'name',  	       	                      
   	       	                      anchor:'100%'
   	       	                    }
   	       	                  ]
   	       	                }
   	       	             ]
   	       	           },{
   	       	              columnWidth:0.2,
   	       	              items:[
   	       	                 {
   	       	                   layout:'form',
   	       	                   labelWidth:55,
   	       	                   items:[userStatus_comb]
   	       	                 }
						  ]
   	       	           },{
   	                    xtype:'button',
				        text:'查找',
				        handler:searchUserByCondition
   	                 }
   	       	        ]
   	       	      }
   	            ]   	         
   	      });
   	      
   	      var userGrid_store = new Ext.data.JsonStore({
   	  			url:Uec3.App.Constants.homeUrl+"/partUpload/manageList.do",
   	  			root:'root',
   	  			totalProperty:'totalProperty',
   	  		    fields:['id','companyName','code','name','nameEn','measurementUnit','status','carModel','brand','origin'],
   	  		    listeners :{
                  'beforeload':function(){
                       Ext.MessageBox.wait("正在加载数据...","提示");
                   }            
                }
   	      });
   	      
     var store_firstCategory=new Ext.data.JsonStore({
    		url:Uec3.App.Constants.homeUrl+"/part/category_getFirstCategorys.do",
    		root:'root',
    		totalProperty: "totalProperty",
			baseParams:{start:0,limit:10},
    		fields:[
    			{name : 'id',type:'int'},
    			{name : 'level1Name',type:'string'}
    		]
		}); 	      
/**
 * 一级分类下拉框
 * @param root_url
 * @return
 */
function getFirstCategroyCombox(isBlank) {
var  combo_partner = new Ext.form.ComboBox({
        fieldLabel : '一级类别',
        store: store_firstCategory,
        displayField:'level1Name',
		valueField:'id',
		hiddenName:'level1Id',
        typeAhead: false,
        mode: 'remote',
        triggerAction: 'all',
        emptyText:'请选择',
        minChars:3,
		anchor : '100%',
        selectOnFocus:true,
		forceSelection  :true,
		allowBlank : isBlank
   });
   return combo_partner;

}


     var store_secondCategory=new Ext.data.Store({
						proxy : new Ext.data.HttpProxy({
									url : Uec3.App.Constants.homeUrl
											+ '/part/category_getSubCategoryList.do'
								}),
						reader : new Ext.data.JsonReader({
									root : 'root',
									fields : [{
												name : 'id'
											}, {
												name : 'level2Name'
											}]
								})
					});
           
/**
 * 二级分类下拉框
 * @param root_url
 * @return
 */
function getSecondCategroyCombox(isBlank) {
var  combo_partner = new Ext.form.ComboBox({
        fieldLabel : '二级类别',
        store: store_secondCategory,
        displayField:'level2Name',
		valueField:'id',
		hiddenName:'partCategoryId',
        typeAhead: false,
        mode: 'local',
        triggerAction: 'all',
        emptyText:'请选择',
        minChars:3,
		anchor : '100%',
        selectOnFocus:true,
		forceSelection  :true,
		allowBlank : isBlank
   });
   return combo_partner;

}
   	     
   	     	//创建工具栏组件
		  var toolbar = new Ext.Toolbar({
		     items:[
			    {text : '审核新零件',iconCls : 'icon-edit',handler:gridEdit}			    	 
		     ]
		  });
   	      
          var column = new Ext.grid.ColumnModel({
          		columns:[
          		   	new Ext.grid.RowNumberer(),
      		 	   	{header:'公司名称',dataIndex:'companyName'},
      		 	   	{header:'零件编码',dataIndex:'code'},
      		 	   	{header:'名称',dataIndex:'name'},
      		 		{header:'英文名称',dataIndex:'nameEn'},
      		 		{header:'单位',dataIndex:'measurementUnit'}, 
      		 		{header:'品牌',dataIndex:'brand'},
      		 		{header:'状态',dataIndex:'status',width:80,renderer:Ext.util.Format.comboRenderer(userStatus_comb)},
      		 		{header:'产地',dataIndex:'origin'},     		 		
      		 		{header:'车型',dataIndex:'carModel'}
          		],
          		defaults: {
          			menuDisabled: true         
       			}
          });
      		 
        
         var user_grid = new Ext.grid.GridPanel({
      		 viewConfig:{forceFit:true,scrollOffset:20},	   	   
	   	   	 autoScroll : true,
	   	   	 titleCollapse:true,
	         colModel:column,
	         height:250,
	         selModel:new Ext.grid.RowSelectionModel({moveEditorOnEnter:true,singleSelect :true}),
	         store:userGrid_store,	           
	         columnLines :true,
	         tbar:toolbar,
	         bbar: new Ext.PagingToolbar({
		            pageSize:19,
	                store:userGrid_store,
	                displayInfo: true,
	                displayMsg: '第 {0} - {1} 条  共 {2} 条',
                    emptyMsg: "没有记录",                   
                    listeners:{
                        beforechange:function(){
                      		 Ext.MessageBox.wait("正在加载数据...","提示");	                     
                        },
                        change:function(){
                            Ext.MessageBox.hide();
                        }
                    }
		     })
         });
         
         var join_panel= new Ext.Panel({
   	         border:false,
   	         
   	         items:[search_form,user_grid]
   	    });
   	    
         
         function searchUserByCondition(){
            var queryParam = Ext.encode(search_form.getForm().getValues());
            loadUser(queryParam);
         }
         
         function loadUser(queryParam){
         	 if(queryParam){
				 userGrid_store.baseParams={start:0,limit:19,queryParam:queryParam}; 
			 }else{
			 	 userGrid_store.baseParams={start:0,limit:19}; 				 
			 }
			 userGrid_store.load({callback:function(records,options,success){
	              if(success){
	                 var total=records.length;
	                 if(Ext.isEmpty(total)){
	                    Ext.MessageBox.alert("提示","未找到相关记录");
	                 }
	              }else{
	                 Ext.MessageBox.alert("提示","数据加载异常...");
	              }
		    }});
         }
         
        var ds_measurementUnit_select = new Ext.data.JsonStore({
					url : Uec3.App.Constants.homeUrl + "/part/unit_getUnits.do",					
					fields : [{name : 'name'}]
		}); 
		
		var firstCategory_cmb=getFirstCategroyCombox(false);
		
		firstCategory_cmb.on('select', function(combo) {
            secondCategory_cmb.clearValue();
			secondCategory_cmb.getStore().removeAll();
			secondCategory_cmb.getStore().setBaseParam('level1Id',combo.getValue());
			secondCategory_cmb.getStore().load();
		 });
		
		var secondCategory_cmb=getSecondCategroyCombox(false);
		
        var initialItem_form_add = new Ext.FormPanel({
		labelAlign : 'right',
		labelWidth : 80,
		bodyStyle : 'padding:5px',
		border : false,
		frame:true,
		fileUpload : true,
		baseCls : 'x-plain',
		defaultType : 'textfield',
		height : 'auto',
		defaults:{
			width : 250
		},
		xtype : "form",
		items : [
			{  
				allowBlank : false,
				blankText : '零件编码不能为空',
				name : 'code',
				fieldLabel:'零件编码(*)',  				
   				emptyText : '至少输入三位零件编码'
			    },{
				xtype:'combo',				    				
				allowBlank : false,
				blankText : '单位不能为空',
				name : 'measurementUnit',					
				fieldLabel:'单位(*)',
				valueField:'name',
	            displayField : 'name',
   	  			mode : 'remote',
   	  			queryParam :'queryParam',
   	  			queryDelay:500,
   	  			minChars :1,
   	  			store : ds_measurementUnit_select,  	  								 
   	  			pageSize:10,
	   			selectOnFocus : true,
	   			triggerAction : 'all',
	   			loadingText : '加载中...',	   				
	   			emptyText : '请输入至少1个字符',
	   			listWidth:270,
	   			focusOrder:1
		   }, {
				fieldLabel : '零件名称(*)',
				allowBlank : false,
				name : 'name'
			}, {
				fieldLabel : '英文名称(*)',
				allowBlank : false,
				name : 'nameEn'
			},firstCategory_cmb,
			secondCategory_cmb,{
				fieldLabel : '车型',
				name : 'carModel'
			}, {
				fieldLabel : '品牌',
				name : 'brand'
			}, {
				fieldLabel : '产地',
				name : 'origin'
			},{
				fieldLabel : '不通过理由',
				name : 'errorMsg'
			},{
			xtype:'hidden',
			name:'id'
			}
		],
		buttons : [{
				text : '保存',
				iconCls:'icon-save',
				handler :submitToNewPart
			},{
				text : '不通过',
				iconCls:'icon-close',
				handler :submitNotPass
			}, {
				text : '关闭',
				iconCls:'icon-close',
				handler :function(){
					initialItem_win_add.hide();
				}
		    }],
		buttonAlign:'center'
		
	}); 
         
        var initialItem_win_add =new Ext.Window(
		{
			layout:'fit',
		    width:430,
		    closeAction:'hide',
		    height:330,
		    title:"新增零件编码-ESC关闭",
			resizable :true,
			shadow : true,
			modal :true,
		    closable:true,
		    bodyStyle:'padding:5 5 5 5',
		    animCollapse:true,
			items : [ initialItem_form_add ]

	}); 
         
        function gridEdit() {
		var record = user_grid.getSelectionModel().getSelected();
		if (record) {
			
            initialItem_form_add.getForm().reset();
			if(record.data.status == 1){
               		initialItem_win_add.isAdd=false;
			        initialItem_win_add.setTitle("编辑零件编码-ESC关闭");
			        initialItem_form_add.getForm().loadRecord(record);
			        initialItem_win_add.show();

                 }else{
                    Ext.MessageBox.alert("提示","该记录已经审核!")
                 }
			
			
		} else {
			Ext.MessageBox.alert("提示","请选需要审核的数据");
		}
	}
         
	
	
	function  submitToNewPart(){
		
		var frm = initialItem_form_add.form;
		
		
		if(!frm.isValid()){
			return false;
		}

		var json=frm.getValues();
		json.level1Name=firstCategory_cmb.getRawValue();
		json.level2Name=secondCategory_cmb.getRawValue();
		Ext.Ajax.request({
					waitMsg:'请稍后.....',
					waitTitle:'提示',
					url:Uec3.App.Constants.homeUrl+'/partUpload/submitToPart.do',
			        method :"post",	
			        params : {
						queryParam : Ext.util.JSON.encode(json)
				    },
			        success:function(response,options){
			        	var result = Ext.decode(response.responseText);
			        	if(result.success){
			        		userGrid_store.reload();
			        		Ext.Msg.alert("信息",result.msg);
                            initialItem_win_add.hide();
			        	}else{
			        		
			        		Ext.Msg.alert("信息",result.msg);
			        	}
			        },
			      failure:function(form,action){
			          Ext.Msg.alert('提示','操作失败  ');
			      }
			});
	}
	
	
	 function submitNotPass(){
		
		var frm = initialItem_form_add.form;
		if(frm.findField("errorMsg").getValue()==""){
			 Ext.Msg.alert('提示','请填写不通过原因');
			return false;
		}
		

		Ext.Ajax.request({
					waitMsg:'请稍后.....',
					waitTitle:'提示',
					url:Uec3.App.Constants.homeUrl+'/partUpload/submitNotPass.do',
			        method :"post",	
			        params : {
						queryParam : Ext.util.JSON.encode(frm.getValues())
				    },
			        success:function(response,options){
			        	var result = Ext.decode(response.responseText);
			        	if(result.success){
			        		userGrid_store.reload();
			        		Ext.Msg.alert("信息",result.msg);
                            initialItem_win_add.hide();
			        	}else{
			        		
			        		Ext.Msg.alert("信息",result.msg);
			        	}
			        },
			      failure:function(form,action){
			          Ext.Msg.alert('提示','操作失败  ');
			      }
			});
	}
       
         
        function init(){
 	       
   	       loadUser();
   	    } 
   	    
   	    init();
   	    
   	   
   	    return join_panel;
   },
   
   /**
	* Create this modules window here.
	*/
   createWindow : function(params){
    	var desktop = this.app.getDesktop();
        var win = desktop.getWindow('partUploadManage');
        if (!win) {
			win = desktop.createWindow({
				        id : "partUploadManage",
						title : '新提交零件编码管理',
						width : 1000,
						height : 500,
						iconCls : 'acc-icon',
						maximizable : true,
						maximized:true,
						constrainHeader : true,
						taskbuttonTooltip : "新提交零件编码管理",
						items : [this.getComponent(params)]
					});
		}
		win.get(0).get(1).setHeight(win.getHeight()-70);
    	win.show(); 
        win.on('resize',function(win,width,height){
      	   win.get(0).get(1).setHeight(height-72);
        });  

   }
});